<?php 
class Checkout extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('order_model');
        $this->output->enable_profiler();
    }
    function index()
    {
        if($data['post']=$this->input->post())
        {
            $this->load->library('form_validation');
            $this->form_validation->set_rules($this->order_model->get_rules());
            if($this->form_validation->run() === true)
            {
                $data['carts']=$this->cart->contents();
                // echo '<pre>'; var_dump($data['cart']); echo '</pre>';die;
                foreach($data['carts'] as $cart)
                {
                    $data = array (
                        'order_name' =>$this->input->post('order_name'),
                        'order_email' =>$this->input->post('order_email'),
                        'order_phone_no' =>$this->input->post('order_phone_no'),
                        'order_deliver_method' =>$this->input->post('order_deliver_method'),
                        'order_address' =>$this->input->post('order_address'),
                        'order_comment' =>$this->input->post('order_comment'),
                        'order_product_name' => $cart['name'],
                        'order_product_price' =>$cart['price'],
                        'order_quantity' =>$cart['qty'],
                        'order_product_id' =>$cart['id'],
                        'order_product_shipping' =>$cart['product_shipping'],
                        'order_total_cost' =>$cart['subtotal']
                    );
                    //echo '<pre>'; var_dump($data); echo '</pre>';die;
                    $this->order_model->save($data);
                }
                $this->session->set_userdata('order_name',$this->input->post('order_name'));
                $this->session->set_userdata('order_email',$this->input->post('order_email'));
                $this->session->set_userdata('order_phone_no',$this->input->post('order_phone_no'));
                $this->session->set_userdata('order_deliver_method',$this->input->post('order_deliver_method'));
                $this->session->set_userdata('order_address',$this->input->post('order_address'));
                $this->session->set_userdata('order_comment',$this->input->post('order_comment'));
                redirect('checkout/donhang');
            }
        }
        $this->load->view('fontend/product/form_cat');
    }
    function donhang()
    {
        $data['carts']=$this->cart->contents();
        $data['total_all']=$this->cart->total();
        $data['total_shipping']=$this->cart->total_shipping();
        
        $this->load->view('fontend/product/donhang',$data);
    }
}